package com.hepinghospital.user.ui.activity.home;

import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hepinghospital.user.R;
import com.hepinghospital.user.adapter.DeptAdapter;
import com.hepinghospital.user.base.BaseActivity;
import com.hepinghospital.user.util.ToastUtil;
import com.rest.business.RestProxy;
import com.rest.observer.BaseObserver;
import com.rest.response.HomeDeptResponse;
import com.scwang.smartrefresh.layout.api.RefreshLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import jz.joyoung.robot.util.DpUtil;

/**
 * 首页找科室 更多
 *
 * @author lzp
 * @time 2023/03/29 13:54
 */

public class HomeDeptMoreActivity extends BaseActivity {
    @BindView(R.id.refreshLayout)
    RefreshLayout refreshLayout;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.btn_add_family)
    TextView btnAddFamily;
    List<HomeDeptResponse> list = new ArrayList<>();
    private DeptAdapter deptAdapter;

    @Override
    protected void initViews() {
        tvTitle.setText("全部科室");
        GridLayoutManager managerDept = new GridLayoutManager(this, 4);
        recyclerView.setLayoutManager(managerDept);
        deptAdapter = new DeptAdapter(list);
        recyclerView.setAdapter(deptAdapter);
        recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
                super.onDraw(c, parent, state);
            }

            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
                outRect.right = DpUtil.dip2px(HomeDeptMoreActivity.this, 6);
                outRect.left = DpUtil.dip2px(HomeDeptMoreActivity.this, 6);
                outRect.bottom = DpUtil.dip2px(HomeDeptMoreActivity.this, 12);
            }
        });
        deptAdapter.setOnItemClickListener((adapter, view, position) -> {
            Intent intent = new Intent(mContext, SearchDoctorActivity.class);
            intent.putExtra("deptId", list.get(position).id);
            intent.putExtra("deptName", list.get(position).name);
            intent.putExtra("type", 0);
            mContext.startActivity(intent);
        });
        refreshLayout.setEnableLoadMore(false);
        refreshLayout.setOnRefreshListener(refreshlayout -> getHomeDeptList());
    }

    @Override
    protected void initData() {
        getHomeDeptList();
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_dept_list;
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    private void getHomeDeptList() {
        RestProxy.getInstance().getHomeDeptList("1", new BaseObserver<List<HomeDeptResponse>>() {
            @Override
            public void onError(Throwable e) {
                refreshLayout.finishRefresh(false);
                ToastUtil.onError(HomeDeptMoreActivity.this, e);
            }

            @Override
            public void onSuccess(List<HomeDeptResponse> homeDeptResponses) {
                list.clear();
                list.addAll(homeDeptResponses);
                deptAdapter.notifyDataSetChanged();
            }
        });
    }
}
